package com.ruoyi.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.mchange.v2.util.CollectionUtils;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.UserDeclarationMapper;
import com.ruoyi.system.domain.UserDeclaration;
import com.ruoyi.system.service.IUserDeclarationService;
import com.ruoyi.common.core.text.Convert;

import javax.validation.constraints.AssertTrue;

/**
 * 报单Service业务层处理
 *
 * @author triangle
 * @date 2022-07-28
 */
@Service
public class UserDeclarationServiceImpl implements IUserDeclarationService {
    @Autowired
    private UserDeclarationMapper userDeclarationMapper;

    /**
     * 查询报单
     *
     * @param decId 报单主键
     * @return 报单
     */
    @Override
    public UserDeclaration selectUserDeclarationByDecId(Long decId) {
        return userDeclarationMapper.selectUserDeclarationByDecId(decId);
    }

    /**
     * 查询报单列表
     *
     * @param userDeclaration 报单
     * @return 报单
     */
    @Override
    public List<UserDeclaration> selectUserDeclarationList(UserDeclaration userDeclaration) {
        return userDeclarationMapper.selectUserDeclarationList(userDeclaration);
    }

    /**
     * 新增报单
     *
     * @param userDeclaration 报单
     * @return 结果
     */
    @Override
    public int insertUserDeclaration(UserDeclaration userDeclaration) throws Exception {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        userDeclaration.setCreateBy(sysUser.getUserName());
        userDeclaration.setCreateTime(DateUtils.getNowDate());
        UserDeclaration userDeclaration1 = new UserDeclaration();
        userDeclaration1.setDecOrderNo(userDeclaration.getDecOrderNo());
        List<UserDeclaration> userDeclarations = userDeclarationMapper.selectUserDeclarationList(userDeclaration1);
        if (null != userDeclarations && !userDeclarations.isEmpty()) {
            throw new Exception("已存在单号" + userDeclaration.getDecOrderNo());
        }
        return userDeclarationMapper.insertUserDeclaration(userDeclaration);
    }

    /**
     * 修改报单
     *
     * @param userDeclaration 报单
     * @return 结果
     */
    @Override
    public int updateUserDeclaration(UserDeclaration userDeclaration) {
        SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
        userDeclaration.setUpdateBy(sysUser.getLoginName());
        userDeclaration.setUpdateTime(DateUtils.getNowDate());
        return userDeclarationMapper.updateUserDeclaration(userDeclaration);
    }

    /**
     * 批量删除报单
     *
     * @param decIds 需要删除的报单主键
     * @return 结果
     */
    @Override
    public int deleteUserDeclarationByDecIds(String decIds) {
        return userDeclarationMapper.deleteUserDeclarationByDecIds(Convert.toStrArray(decIds));
    }

    /**
     * 删除报单信息
     *
     * @param decId 报单主键
     * @return 结果
     */
    @Override
    public int deleteUserDeclarationByDecId(Long decId) {
        return userDeclarationMapper.deleteUserDeclarationByDecId(decId);
    }
}
